<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="cache-control" content="max-age=0"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/common/admin_templ_style.css"/>
    <link rel="stylesheet" type="text/css" href="../api/css/cupertino/jquery-ui-1.10.1.custom.min.css"/>
    <link rel="stylesheet" type="text/css" href="../api/css/jquery.colorpicker.css"/>
    <script src="../api/js/jquery-1.8.3.js" type="text/javascript"></script>
    <script src="../api/js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="../api/js/jquery.colorpicker.js"></script>
    <script type='text/JavaScript' src='../../zbw_reporting/scripts/framework_include.js'></script>

    <script type="text/javascript">

        $(function () {
            $(".hidden-field").hide();

            //Select show/hide option
            $("select#banner-font").bind('change', function(){
                var thisValue = $(this).val();
                if (thisValue != "")
                    $("."+thisValue).show();
            });

            $('span.close').on('click', function(){
                $(this).closest('.hidden-field').hide();
            });

            $('#font-color, #left-panel-bg-color').colorpicker(
                    {
                        parts:'full',
                        alpha:true,
                        showOn:'both',
                        buttonColorize:true,
                        showNoneButton:true
                    });
        });
        function generateAccordion(){
            var test = $('<div id="accordion3"></div>');
            var h3 = '<h3>Banner</h3>';
            var content = '<div> </div>';

            for(var x=0; x < 5; x++)
            {
                $(h3).appendTo(test);
                $(content).appendTo(test);
            }
            $(test).appendTo("body");




        }

        var propertiesList = [];
        function getData() {
            $.ajax({
                type: "GET",
                data: {
                },
                url: "/GreenBI/admin/ThemeEngine/mydata.json",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    //manipulate the data
                    var result = data.root;
                    propertiesList = GetAllProperties(result);
                    var accord = buildAccordion(result);
                    $(accord).appendTo("#div1");

                },
                error: function (error) {
                    // show the error details or show a user friendly error message
                    alert("Due to unexpected errors we were unable to load data");

                }
            });
        }

        function GetAllProperties(data)
        {
            var result = [];
            for(var x = 0; x < data.length; x++)
            {
                for(var y = 0; y < data[x].properties.length; y++)
                {

                    result.push(data[x].properties[y]);

                }
            }
            return result;
        }


        function BuildField(obj) {
            var label = $('<label>'  + obj.label + '</label>');
            var content = $('<div></div>');

            $(label).appendTo(content);
            content.append( BuildInputComponent(obj) );

            return content;
        }

        function BuildInputComponent(obj)
        {
            if(obj.selectionType ===  "text")
                return BuildTextInput(obj);
            else if(obj.selectionType ===  "select")
                return BuildDropDownSelect(obj);
            return ;
        }


        function InputComponentChangeHandler(e)
        {
            if(e.currentTarget.type === "text")
                TextInputChangeHandler(e);
            else if(e.currentTarget.type === "select-one")
                SelectInputChangeHandler(e);
        }

        function SelectInputChangeHandler(e)
        {
            var value = e.currentTarget.value;
            var id = e.currentTarget.id;

            //up key
            if (e.keyCode == '38') {

            }

            //down down
            else if(e.keyCode == '40') {

            }

            ApplyCssChanges(id, value, propertiesList);
        }

        function TextInputChangeHandler(e)
        {
            var value = e.currentTarget.value;
            var id = e.currentTarget.id;
            ApplyCssChanges(id, value, propertiesList);
        }

        function ApplyCssChanges(id, newValue, propertiesList)
        {
            for(var x = 0; x < propertiesList.length; x++)
            {
                if(id === propertiesList[x].id)
                {
                    var obj = propertiesList[x];
                    $(obj.selectors).css(obj.css, newValue);
                    break;
                }
            }
        }

        function BuildTextInput(obj) {
          var content =   $("<input/>",
                                {   id : obj.id,
                                    type: obj.selectionType,
                                    name: obj.value,
                                    value: obj.value,
                                    keyup: InputComponentChangeHandler
                                });
            return content;
        }
        /* need to be tested */
        function BuildDropDownSelect(obj) {
            //obj is the list of option values
                var content = $('<select/>',
                    {
                            id : obj.id,
                            change: InputComponentChangeHandler
                    });
                for(var x = 0; x < obj.options.length; x++)
                {
                    content.append(
                            $('<option>'+ obj.options[x] + '</option>',
                                    { value:obj.options[x]
                                    }
                            )
                    );
                }
            return content;
        }


        /* Bulding Accordion */
        function buildAccordion(result) {
            var accordion = $('<div id="accordion3"></div>');
            for(var x = 0; x < result.length; x++)
            {
                var h3 = '<h3>' + result[x].name + '</h3>';
                $(h3).appendTo(accordion);
                var properties = "";
                var content = $('<div></div>');
                for(var y=0; y < result[x].properties.length; y++)
                {
                    var field = BuildField(result[x].properties[y]);
                    $(field).appendTo(content);
                }
                $(content).appendTo(accordion);
            }

            $(accordion).appendTo("body");
            $(accordion).accordion({collapsible: true, heightStyle: "content", animate: 200});
            return $(accordion);
        }


        function GetCSS(propertiesList, showEmpty) {
            var result = "";
            for(var x = 0; x < propertiesList.length; x++)
            {
                if(showEmpty)
                {
                    result += GetSingleCSSProp(propertiesList[x]) + "\n";
                }
                else
                {
                    if(propertiesList[x].value != "")
                        result += GetSingleCSSProp(propertiesList[x]) + "\n";
                }
            }
            return result;
        }

        function GetSingleCSSProp(data) {
            var result = data.selectors + "{" + data.css + ":" + data.value  + "}";
            return result;
        }

        $( document ).ready(function() {
            getData();
            $( "#tabs" ).tabs();
            $( "#tabs_preview" ).tabs();

            $( "#div1" ).css("height", $(window).height()-128);


        });



        function SaveCSS(propertiesList, showEmpty){
            var result = GetCSS(propertiesList, showEmpty);
            alert(result);
        }


    </script>
    <style>
        body {
            background:none;
            height: 100%;
        }
        #theme-edit-form {
            height: calc(100% - 42px);
        }
        #theme-edit-form h2 {
            margin-bottom:20px;
        }
        #theme-edit-form h3.ui-accordion-header-active {
            color:#fff;
        }
        #theme-edit-form select {
            padding-left:0;
            white-space:nowrap;
        }
        #theme-edit-form input[type="file"]{
            width: 132px;
        }
        .theme-edit-block {
            margin-left: 10px;
            width: 21%;
        }
        .ui-accordion .theme-edit-content {
            padding:0;
        }
        .theme-edit-content ul {
            padding: 20px 5px;
        }
        .theme-edit-content li label {
            font-size:11px;
            padding-right:10px;
            width:35%;
        }
        .theme-edit-content button.ui-state-default {
            background:none;
            border: none;
        }
        .theme-edit-content span.ui-button-text {
            padding: 0;
            font-style: inherit;
        }
        .hidden-field {
            margin: 5px 0;
        }
        .hidden-field input {
            width:60px;
        }
        .hidden-field select {
            width: 77px;
        }
        .hidden-field span.close {
            padding: 1px 5px;
            display:inline-block;
            background:#aed0ea;
            color: #fff;
            border-radius:3px;
            margin-left:5px;
        }
        .hidden-field span.close:hover {
            cursor:pointer;
            background: #4F7891;
        }

        form, .kpi-preview, .kpi-drill {
            border: 1px solid #9CD6E7;
            border-radius: 5px;
            box-shadow: 0 0 100px #AADCEA;
            margin: 0 auto;
            padding-bottom: 50px;
            width: 98%;
        }

        /*  new */
        .left-25 {
            float: left;
            width: calc(30% - 10px);
            height: calc(100% - 10px);
        }

        .right-75 {
            float: left;
            height: calc(100% - 10px);
            margin-left: 20px;
            width: calc(70% - 10px);
        }

        .tab-frame label {
            margin-left: -33px;
            width: 33%;
        }

        .tab-frame label {
            width: 48%;
        }

        input[type = "text"], select, textarea {
            width: 50%;
        }

        #accordion3 div:not(:first-child) {
            padding-top: 8px;
        }

        .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
            font-size: 12px;
            padding: 1px 1px 1px 27px;
        }

        #bi_admin_layout_editor .top-content {
            height: 100%;
        }


        #bi_admin_layout_editor .top-content {
          padding: 0 1px;
        }


    </style>
</head>
<body>
<a href="#" onclick="SaveCSS(propertiesList, false)"> Save CSS</a>
<div class="tab-frame" id="bi_admin_layout_editor">

    <div class="tab-form">
        <div class="top-content">
                <div class="left-25">
                    <div id="tabs">
                        <ul>
                            <li><a href="#div1">Theme Builder</a></li>
                            <li><a href="#div2">Gallery</a></li>
                        </ul>
                        <div id="div1">

                        </div>
                        <div id="div2">

                        </div>

                    </div>

                </div>
                <div class="right-75"><span id="previewPane">



                    <div id="tabs_preview">
                        <ul>
                            <li><a href="#tabs-1">Nunc tincidunt</a></li>
                            <li><a href="#tabs-2">Proin dolor</a></li>
                            <li><a href="#tabs-3">Aenean lacinia</a></li>
                        </ul>
                        <div id="tabs-1">
                            <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
                        </div>
                        <div id="tabs-2">
                            <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
                        </div>
                        <div id="tabs-3">
                            <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
                            <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
                        </div>
                    </div>

                </span></div>
        </div>
    </div>
</div>
</body>
</html>